<template>
  <ant-modal
    modalWidth="900"
    modalHeight="600"
    :visible="open"
    v-if="open"
    modal-title="选择项目"
    :adjust-size="true"
    :maskClosable="false"
    @cancel="cancel"
  >  
  <div slot="content">
  <a-card :bordered="false" style="margin-bottom: 10px;" >
        <!-- 条件搜索 -->
        <div class="table-page-search-wrapper">
          <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
            <a-row :gutter="32">
              <a-col :span="12">
                <a-form-item label="项目编号">
                  <a-input type='text' placeholder="请输入" v-model="queryParam.projectNumber" />
                </a-form-item>
              </a-col>
              <a-col>
                <span class="table-page-search-submitButtons" style="float: right;">
                  <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                  <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </a-card>
      <a-card :bordered="false" class="table-card">
        <!-- 新增/修改 -->
        <biz-project-add-form v-if="showAddModal" ref="bizProjectAddForm" @ok="getList" @close="showAddModal = false" />
        <!-- 增加 -->
        <biz-order-add-form
          v-if="showOrderModal"
          ref="bizOrderAddForm"
          :customerIdOptions="customerIdOptions"
          :isSettleSendOptions="isSettleSendOptions"
          :orderTypeOptions="orderTypeOptions"
          @ok="getList"
          @close="showOrderModal = false"
        />
        <advance-table 
        :components="drag(columns)"
        title="项目列表" :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }" tableKey="base-bizOrder-index-table" @change="handleTableChange" rowKey="id" @refresh="getList"
          :row-selection="{ selectedRowKeys: selectedRowKeys,type:'radio', onChange: onSelectChange,hideDefaultSelections:true }"
          :columns="columns" :data-source="dataList" :loading="loading" :format-conditions="true">
          <span
            slot="expandedRowRender"
            slot-scope="{ record}"
          >
            <biz-project-index
              ref="BizProjectIndex"
              title="项目列表"
              :projectNumber="record.projectNumber"
              :dataList="record.bizOrders"
            />
          </span>
        </advance-table>
      </a-card>
    </div>
    
    <template slot="footer">
      <a-button type="primary" @click="submitForm(true)">
        确定
      </a-button>
      <a-button @click="cancel">
        取消
      </a-button>
    </template>
  </ant-modal>
</template>


<style scoped>

</style>
  <script>
  import AntModal from '@/components/pt/dialog/AntModal'
  import { listBizProject, delBizProject } from '@/api/biz/bizProject'
  import { getInitData } from '@/api/biz/bizOrder'
  import { listAllBizCustomer } from '@/api/biz/bizCustomer'
  import AdvanceTable from '@/components/pt/table/AdvanceTable'
  import BizProjectAddForm from '@/views/biz/bizproject/modules/BizProjectAddForm'
  import BizOrderAddForm from '@/views/biz/bizorder/modules/BizOrderAddForm'
  import BizProjectIndex from '@/views/biz/bizproject/BizProjectIndex'
  import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
  export default {
    name: 'BizProject',
    components: {
      AdvanceTable,
      BizProjectAddForm,
      BizProjectIndex,
      BizOrderAddForm,
      AntModal,
    },
    mixins:[tableDragResizeMixin],
    data() {
      return {
        showAddModal: false,
        showOrderModal: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 选中的主键集合
        selectedRowKeys: [],
        // 选中的数据集合
        selectedRows: [],
        // 高级搜索 展开/关闭
        advanced: false,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // label的百分比
        labelCol: { span: 6 },
        // 内容区域的百分比
        wrapperCol: { span: 18 },
        // 表格数据
        dataList: [],
        open:false,
        // 查询参数
        queryParam: {
          pageNum: 1,
          pageSize: 10,
          projectNumber: undefined
        },
        columns: [
        {
            title: '项目日期',
            dataIndex:'projectDate',
            align: 'center'
          },
          {
            title: '项目编号',
            dataIndex:'projectNumber',
            align: 'center'
          },
          {
            title: '备注',
            dataIndex:'remark',
            align: 'center'
          },
        ]
      }
    },
    created() {
      
    },
    methods: {
      show(){
        this.getList()
        getInitData('order_type,sys_yes_no,order_type').then(response => {
            this.isSettleSendOptions = response.data.sys_yes_no
            this.orderTypeOptions = response.data.order_type
        })
        listAllBizCustomer().then(response=>{
            this.customerIdOptions=response.data;
        })
        this.open = true
      },
      /** 查询销售单列表 */
      getList() {
        this.loading = true
        listBizProject(this.queryParam).then(response => {
          this.dataList = response.data.list
          this.total = response.data.total
          this.loading = false
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParam.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParam = {
          pageNum: 1,
          pageSize: 10,
          projectNumber: undefined
  
        }
        this.handleQuery()
      },
      handleOrderAdd (record) {
        this.showOrderModal = true
        let row = {projectId:record.id,projectNumber:record.projectNumber}
        this.$nextTick(() => (
          this.$refs.bizOrderAddForm.handleAdd(row)
        ))
      },
      /** 翻页操作 */
      onShowSizeChange(current, pageSize) {
        this.queryParam.pageSize = pageSize
        this.getList()
      },
      /** 翻页操作 */
      onSizeChange(current, size) {
        this.queryParam.pageNum = 1
        this.queryParam.pageSize = size
        this.getList()
      },
      /** 翻页操作 */
      changeSize(current, pageSize) {
        this.queryParam.pageNum = current
        this.queryParam.pageSize = pageSize
        this.getList()
      },
      /** 翻页操作 */
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
        this.ids = this.selectedRows.map(item => item.id)
        this.single = selectedRowKeys.length !== 1
        this.multiple = !selectedRowKeys.length
      },
      /** 查询折叠和展开操作 */
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      handleAdd(record) {
        this.showAddModal = true
        this.$nextTick(() => (
          this.$refs.bizProjectAddForm.handleAdd(record)
        ))
      },
      handleUpdate(record, ids) {
        this.showAddModal = true
        this.$nextTick(() => (
          this.$refs.bizProjectAddForm.handleAdd(record)
        ))
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        var that = this
        this.$confirm({
          title: '确认删除所选中数据?',
          onOk() {
            return delBizProject(row.id)
              .then(() => {
                that.onSelectChange([], [])
                that.getList()
                that.$message.success(
                  '删除成功',
                  3
                )
              })
          },
          onCancel() { }
        })
      },
      handleTableChange(pagination, filters, sorter) {
        if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
          this.queryParam.orderByColumn = 'a.' + sorter.field
          this.queryParam.isAsc = sorter.order
        }
        this.getList()
      },
      cancel(){
        this.open = false
      },
      submitForm(flag){
        this.$emit('ok',this.selectedRows)
        this.cancel()
      }
    }
  }
  </script>
  